﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using QuickSL;
using System.Threading;

namespace WP7Demo
{
    public partial class UI : PhoneApplicationPage
    {
        public UI()
        {
            InitializeComponent();
        }

        private bool _isReset = false;
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            _isReset = true;
            DispatcherQueue.Current.Clear();
        }

        private void btnNormal_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("This may cause the App to crack!", "Alert", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                _isReset = false;
                for (int i = 0; i < 1000; i++)
                {
                    string count = i.ToString();
                    Dispatcher.BeginInvoke(() =>
                    {
                        Add();
                        txtCount.Text = count;
                    });

                    if (_isReset)
                        break;
                }
            }
        }

        private void btnQuickSL_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 1000; i++)
            {
                string count = i.ToString();
                DispatcherQueue.Current.Enqueue(() =>
                {
                    Add();
                    txtCount.Text = count;
                });
            }
        }

        private void Add()
        {
            recLayout.Children.Add(GetRectangle());

            Thread.Sleep(10);//simulate a long-time process
        }

        private Random _rndPosition = new Random(DateTime.Now.Millisecond);
        private Rectangle GetRectangle()
        {
            Rectangle ctrl = new Rectangle();
            ctrl.Width = 5;
            ctrl.Height = 5;
            ctrl.Fill = new SolidColorBrush(Colors.Red);
            ctrl.HorizontalAlignment = HorizontalAlignment.Left;
            ctrl.VerticalAlignment = VerticalAlignment.Top;
            ctrl.Margin = new Thickness(_rndPosition.Next(0, (int)recLayout.ActualWidth), _rndPosition.Next(0, (int)recLayout.ActualWidth), 0, 0);
            return ctrl;
        }
    
    }
}